package com.viewpagerindicator.sample.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.viewpagerindicator.sample.CutokApplication;
import com.viewpagerindicator.sample.R;
import com.viewpagerindicator.sample.util.URLs;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import cz.msebera.android.httpclient.Header;

/**
 * Created by zhao on 2015/11/22.
 */
public class RegisterDialog extends Dialog {

    Button okButton;
    Button cancleButton;
    EditText usernameEditText;
    EditText passwordEdittext;
    TextView loginMsgTextView;
    CutokApplication application;
    View progressBg;
    public interface  LoginEventLinster{
        public void logineed(boolean isSuccess);
    }

    public LoginEventLinster loginEventLinster;
    public RegisterDialog(Context context, LoginEventLinster loginEventLinster) {
        super(context,R.style.LoginDialog);
        this.loginEventLinster=loginEventLinster;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_dialog);
        setTitle(R.string.register_dialog_title);
        application= (CutokApplication) getContext().getApplicationContext();
        okButton= (Button) findViewById(R.id.login_btn_ok);
        cancleButton= (Button) findViewById(R.id.login_btn_cancle);
        usernameEditText= (EditText) findViewById(R.id.username);
        passwordEdittext= (EditText) findViewById(R.id.password);
        progressBg=findViewById(R.id.logint_progress_bg);
        loginMsgTextView= (TextView) findViewById(R.id.lgoin_error_msg);
        cancleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RegisterDialog.this.dismiss();
            }
        });
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = usernameEditText.getText().toString();
                String password = passwordEdittext.getText().toString();
                if (username == null || "".equals(username.trim())) {
                    usernameEditText.setHint("Username Cant Be Null");
                    return;
                }
                if (password == null || "".equals(password.trim())) {
                    passwordEdittext.setHint("Password Cant Be Null");
                    return;
                }
                toLogin(username,password);
            }
        });
    }

    public void toLogin(final String username,String password){
        RequestParams requestParams=new RequestParams();
        requestParams.put("user_login",username);
        requestParams.put("password", password);
        progressBg.setVisibility(View.VISIBLE);
        AsyncHttpClient client = new AsyncHttpClient();
        client.post(getContext(), URLs.register, requestParams, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                JSONTokener jsonParser = new JSONTokener(new String(responseBody));
                try {
                    JSONObject loginJsonObj = (JSONObject) jsonParser.nextValue();
                    Integer status=loginJsonObj.getInt("status");
                    String msg=loginJsonObj.getString("msg");
                    if(status<0){
                        loginMsgTextView.setText(msg);
                    }else{
                        application.loginSuccess(msg,username);
                        if(isShowing()){
                            dismiss();
                        }
                        loginEventLinster.logineed(true);
                        Toast.makeText(getContext(),msg,Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    loginMsgTextView.setText(R.string.error_msg_login_error);
                }finally {

                    progressBg.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                loginMsgTextView.setText(R.string.error_msg_login_error);
                progressBg.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void show() {
        super.show();
        WindowManager windowManager = getWindow().getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = (int)(display.getWidth())-40; //设置宽度
        this.getWindow().setAttributes(lp);
    }
}
